package com.ruoyi.industry.controller.back;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.industry.domain.ProjectAuction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.industry.domain.ProjectBuilding;
import com.ruoyi.industry.service.IProjectBuildingService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 项目-楼宇办公/商用Controller
 *
 * @author MagicCube
 * @date 2023-05-08
 */
@RestController
@RequestMapping("/building")
public class ProjectBuildingController extends BaseController
{
    @Autowired
    private IProjectBuildingService projectBuildingService;

    /**
     * 查询项目-楼宇办公/商用列表
     */
    @RequiresPermissions("industry:building:list")
    @GetMapping("/list")
    public TableDataInfo list(ProjectBuilding projectBuilding)
    {
        startPage();
        List<ProjectBuilding> list = projectBuildingService.selectProjectBuildingListz(projectBuilding);
        return getDataTable(list);
    }

    /**
     * 导出项目-楼宇办公/商用列表
     */
    @RequiresPermissions("industry:building:export")
    @Log(title = "项目-楼宇办公/商用", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProjectBuilding projectBuilding)
    {
        List<ProjectBuilding> list = projectBuildingService.selectProjectBuildingList(projectBuilding);
        ExcelUtil<ProjectBuilding> util = new ExcelUtil<ProjectBuilding>(ProjectBuilding.class);
        util.exportExcel(response, list, "项目-楼宇数据");
    }

    /**
     * 导出楼宇免费服务委托书
     */
    @RequiresPermissions("industry:building:export")
    @Log(title = "楼宇免费服务委托书", businessType = BusinessType.EXPORT)
    @GetMapping("/entrust")
    public void entrust(HttpServletResponse response, ProjectBuilding projectBuilding)
    {
        projectBuildingService.exportEntrust(response,projectBuilding.getId());
    }

    /**
     * 下载模板
     * @param response response
     * @throws IOException
     */
//    @RequiresPermissions("industry:building:export")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException{
        List<ProjectBuilding> list = new ArrayList<>();
        list.add(ProjectBuilding.getBuilding());
        ExcelUtil<ProjectBuilding> util = new ExcelUtil<>(ProjectBuilding.class);
//        util.importTemplateExcel(response,"楼宇管理",list);
        util.exportExcel(response, list, "楼宇数据");
    }

    /**
     * 数据导入
     * @param file 文件
     * @param updateSupport 更新支持
     * @return
     * @throws Exception
     */
    @Log(title = "楼宇免费服务委托书", businessType = BusinessType.IMPORT)
    @RequiresPermissions("industry:building:import")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport, Integer buildType) throws Exception {
        ExcelUtil<ProjectBuilding> util = new ExcelUtil<>(ProjectBuilding.class);
        List<ProjectBuilding> projectBuildings = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = projectBuildingService.importUser(projectBuildings, updateSupport, operName, buildType);
        return success(message);
    }

    /**
     * 获取项目-楼宇办公/商用详细信息
     */
    @RequiresPermissions("industry:building:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(projectBuildingService.selectProjectBuildingById(id));
    }

    /**
     * 新增项目-楼宇办公/商用
     */
    @RequiresPermissions("industry:building:add")
    @Log(title = "项目-楼宇办公/商用", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProjectBuilding projectBuilding)
    {
        return toAjax(projectBuildingService.insertProjectBuilding(projectBuilding));
    }

    /**
     * 修改项目-楼宇办公/商用
     */
    @RequiresPermissions("industry:building:edit")
    @Log(title = "项目-楼宇办公/商用", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProjectBuilding projectBuilding)
    {
        return toAjax(projectBuildingService.updateProjectBuilding(projectBuilding));
    }

    /**
     * 删除项目-楼宇办公/商用
     */
    @RequiresPermissions("industry:building:remove")
    @Log(title = "项目-楼宇办公/商用", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(projectBuildingService.deleteProjectBuildingByIds(ids));
    }
}
